package com.demo.sun.t_sensordata;

import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.demo.sun.t_sensordata.Base.BaseActivity;
import com.demo.sun.t_sensordata.Fragment.HomeFragment;
import com.demo.sun.t_sensordata.Fragment.SetFragment;

/**
 * Created by Sun on 2018/5/26.
 */

public class HomeActivity extends BaseActivity implements View.OnClickListener{
    private HomeActivity act;
    private LinearLayout[] lls = new LinearLayout[2];
    private int[] ids = {R.id.layout_home,R.id.layout_set};
    private HomeFragment homeFragment;
    private SetFragment setFragment;


    @Override
    protected int getLayoutId() {
        return R.layout.act_home;
    }

    @Override
    protected void initViews() {
        for (int i = 0;i<lls.length;i++){
            lls[i] = findViewById(ids[i]);
            lls[i].setOnClickListener(this);
        }
    }

    @Override
    protected void initDatas() {
        act=this;
        changeFragment(1);
    }


    @Override
    protected void doViews() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.layout_home:
//                replaceFragment(0);
                changeFragment(0);
                changeBottomTab(0);
                break;
            case R.id.layout_set:
//                replaceFragment(1);
                changeFragment(1);
                changeBottomTab(1);
                break;
        }
    }

    /**
     * 切换Fragment
     * @param pos
     */
    private void changeFragment(int pos){
        FragmentTransaction transaction = act.getSupportFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch (pos){
            case 0:
                if(homeFragment == null){
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.layout_home,homeFragment);
                }else{
                    transaction.show(setFragment);
                }
                break;
            case 1:
                if(setFragment == null){
                    setFragment = new SetFragment();
                    transaction.add(R.id.layout_set,setFragment);
                }else{
                    transaction.show(setFragment);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * 隐藏全部的Fragment
     * @param transaction
     */
    private void hideAllFragment(FragmentTransaction transaction) {
        if(setFragment != null){
            transaction.hide(setFragment);
        }
        if(homeFragment !=null){
            transaction.hide(homeFragment);
        }
    }



    /**
     * 根据点击改变导航栏状态
     * @param position 坐标位置 以0开头
     */
    private void changeBottomTab(int position){
        for (int i = 0;i<lls.length;i++){
            ImageView imageView = (ImageView) lls[i].getChildAt(0);
            TextView textView = (TextView) lls[i].getChildAt(1);
            if(i == position){
                textView.setTextColor(ContextCompat.getColor(act,R.color.color_green));
            }else {
                textView.setTextColor(ContextCompat.getColor(act,R.color.color_255_255_255));
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
